package com.yushealth.prostate.fingerdanceforprostate;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedList;

public class SettingMusicActivity extends AppCompatActivity {

    TextView et_music_volume;
    SeekBar seekbar_music_volume;
    ListView listView;
    String current_music_file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_music);

        et_music_volume = (TextView) findViewById(R.id.et_music_volume);
        seekbar_music_volume = (SeekBar) findViewById(R.id.seekbar_music_volume);
        listView = (ListView) findViewById(R.id.list_music);

        HashMap<String, String> musicData = (HashMap<String, String>) getIntent().getSerializableExtra("music");
        current_music_file = musicData.get("music_file");
        et_music_volume.setText(musicData.get("music_volume"));
        seekbar_music_volume.setMax(100);
        seekbar_music_volume.setProgress(Integer.parseInt(musicData.get("music_volume")));
        seekbar_music_volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                et_music_volume.setText(String.format("%02d", progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        // 列出音乐文件
        LinkedList<String> list = new LinkedList<>();

        Field[] fields = R.raw.class.getFields();
        String field_name;
        int idx_item = 0;

        for (Field field : fields) {
            field_name = field.getName();
            if (field_name.startsWith("$") || field_name.equals("serialVersionUID")) {
                continue;
            }
//            MusicFileData data = new MusicFileData(f.getName());
//            if (f.getName().equals(current_music.toString())) {
//                data.setChecked(true);
//            }
            list.add(field_name);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_single_choice, list);
        listView.setAdapter(adapter);

        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(current_music_file)) {
                listView.setItemChecked(i, true);
            }
        }

        // 返回文件名
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 /*           @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView f = (TextView)view.getTag(R.id.music_item);
                Bundle bundle = new Bundle();
                bundle.putString("file_selected", f.getText().toString());
                String file_name = f.getText().toString();
                        // 显示选择的文件
                current_music.setText(String.format("Current music: %s", file_name));

            }*/

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                listView.setItemChecked(i, true);
                current_music_file = listView.getItemAtPosition(i).toString();
                Toast.makeText(getApplication(), current_music_file, Toast.LENGTH_SHORT).show();
            }
        });

        // action bar home
        ActionBar actionbar;
        if ((actionbar = getActionBar()) != null) {
            actionbar.setDisplayHomeAsUpEnabled(true);
        }
        //
    }


    public void btnApplyClick(View view) {

        Bundle bundle = new Bundle();
        bundle.putString("music_volume", et_music_volume.getText().toString());
        bundle.putString("music_file", current_music_file);
        Intent intent = new Intent();
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    }

    public void btnCancelClick(View view) {
        finish();
    }

    // end
}
